package com.yx.shop.dao.impl;

import com.yx.shop.dao.UserDao;
import com.yx.shop.entitly.User;
import com.yx.shop.utils.JDBCUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;

import java.sql.SQLException;

/**
 * @Classname UserDaoImpl
 * @Description TODO:
 * @Date 2021/05/06 16:54
 * @Created by yx
 */
public class UserDaoImpl implements UserDao {

    private QueryRunner queryRunner = new QueryRunner(JDBCUtils.getDataSource());

    @Override
    public User getUserByName(String name) {
        String sql = "select * from user where uname = ?;";
        try {
            return queryRunner.query(sql,new BeanHandler<>(User.class),name);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public int register(User user) {
        String sql = "insert into user values(default,?,?,?,?,?,?,?);";
        Object[] args={user.getUname(),user.getUpassword(),user.getUemail(),user.getUsex(),user.getUstatus(),user.getUcode(),user.getUrole()};
        try {
            return queryRunner.update(sql,args);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public User getUserByEmail(String email) {
        String sql = "select * from user where uemail = ?;";
        try {
            return queryRunner.query(sql,new BeanHandler<>(User.class),email);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public int setUstatus(int uid, int actSuccess) {
        String sql = "update user set ustatus=? where uid = ?;";
        try {
            return queryRunner.update(sql,actSuccess,uid);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }
}
